package interfaces;

import gameobjects.Teleporter;
import java.awt.Graphics;
import java.util.Comparator;
import land.LandRow;

/** Visible classes can be added to the level to be drawn. */
public interface Visible {
    /**
     * An instance of depth comparator to save creating one every time
     * we need it.
     */
    DepthComparator DEPTH_COMPARATOR = new DepthComparator();

    /** @param g a graphics context to draw to. */
    void draw(final Graphics g);

    /** @return the depth. */
    double getDepth();

    /** Comparator to sort visible objects. */
    public static class DepthComparator implements Comparator<Visible> {
        /**
         * The all important function.
         * 
         * @param a the first object.
         * @param b the second object.
         * @return 0 if the objects are of equal depth, -1 if the
         *         first object should be drawn in front and 1 if the
         *         second object should be drawn in front.
         */
        public final int compare(final Visible a, final Visible b) {
            if (a != null && b != null) {
                int comp = Double.compare(a.getDepth(), b.getDepth());
                if (comp == 0) {
                    if (a instanceof LandRow) {
                        return 1;
                    } else if (b instanceof LandRow) {
                        return -1;
                    } else if (a instanceof Teleporter) {
                        return 1;
                    } else if (b instanceof Teleporter) {
                        return -1;
                    }
                }
                return comp;
            } else {
                return 0;
            }
        }
    }
}
